#include "widget.h"
#include "ui_widget.h"
#include <QTimer>
#include<QDebug>
#include <QMessageBox>


Widget::Widget(QWidget *parent): QWidget(parent), ui(new Ui::Widget){
    ui->setupUi(this);
    resize(800,600);
    setWindowTitle("串口调式助手");

    //周期性扫描串口(定时器)
    timer=new QTimer(this);
    timer->start(5000);
    connect(timer,&QTimer::timeout,this,&Widget::TimerEvent);

    //创建操作串口的对象,用于操作串口
    serial=new QSerialPort();

    //初始化默认的串口信息
    ui->comboBox_2->setCurrentIndex(5);
    ui->comboBox_3->setCurrentIndex(3);
    ui->comboBox_4->setCurrentIndex(2);
    ui->comboBox_5->setCurrentIndex(0);
}

Widget::~Widget(){
    delete ui;
}


//当定时器timeout时，发出信号，触发该槽函数
void Widget::TimerEvent(){
    //获取当前可用的串口信息
    QList<QSerialPortInfo> portInfolist=QSerialPortInfo::availablePorts();

    //用于存放所有可用的串口的名字
    QStringList newPortNameList;
    //是否可以不添加？局部变量？
    newPortNameList.clear();

    foreach(const QSerialPortInfo& info,portInfolist){
        //获取串口名字
        newPortNameList.append(info.portName());

    }

    //----->?
    // //添加之前将combox中的内容清空
    // ui->comboBox->clear();
    // //将获取的串口的名字添加到combox中
    // ui->comboBox->addItems(portNameList);

    if(newPortNameList.size()!=portNameList.size()){
        portNameList=newPortNameList;
        ui->comboBox->clear();
        ui->comboBox->addItems(portNameList);
    }
}

void Widget::on_pushButton_clicked(){
    if(ui->pushButton->text()==QString("打开串口")){
        //设置相关参数
        serial->setPortName(ui->comboBox->currentText());
        serial->setBaudRate(ui->comboBox_2->currentText().toInt());
        switch(ui->comboBox_3->currentText().toInt()){
            case 5:
                serial->setDataBits(QSerialPort::Data5);
                break;
            case 6:
                serial->setDataBits(QSerialPort::Data6);
                break;
            case 7:
                serial->setDataBits(QSerialPort::Data7);
                break;
            case 8:
                serial->setDataBits(QSerialPort::Data8);
                break;
            default:
                serial->setDataBits(QSerialPort::UnknownDataBits);
                break;
        }

        switch(ui->comboBox_4->currentIndex()){
            case 0:
                serial->setParity(QSerialPort::EvenParity);
                break;
            case 1:
                serial->setParity(QSerialPort::OddParity);
                break;
            case 2:
                serial->setParity(QSerialPort::NoParity);
                break;
            default:
                serial->setParity(QSerialPort::UnknownParity);
                break;
        }

        switch(ui->comboBox_5->currentIndex()){
            case 0:
                serial->setStopBits(QSerialPort::OneStop);
                break;
            case 1:
                serial->setStopBits(QSerialPort::OneAndHalfStop);
                break;
            case 2:
                serial->setStopBits(QSerialPort::TwoStop);
                break;
            default:
                serial->setStopBits(QSerialPort::UnknownStopBits);
                break;
        }

        serial->setFlowControl(QSerialPort::NoFlowControl);


        //打开串口
        bool flag=serial->open(QIODevice::ReadWrite);

        if(!flag){
            QMessageBox::information(this,"错误提示","无法打开串口",QMessageBox::Yes);
            return;
        }

        //打开成功之后不可在进行设置
        ui->comboBox->setEnabled(false);
        ui->comboBox_2->setEnabled(false);
        ui->comboBox_3->setEnabled(false);
        ui->comboBox_4->setEnabled(false);
        ui->comboBox_5->setEnabled(false);

        ui->pushButton->setText("关闭串口");
    }else{
        //关闭串口
        serial->close();

        ui->comboBox->setEnabled(true);
        ui->comboBox_2->setEnabled(true);
        ui->comboBox_3->setEnabled(true);
        ui->comboBox_4->setEnabled(true);
        ui->comboBox_5->setEnabled(true);

        ui->pushButton->setText("打开串口");
    }

}

